package com.toggery.POS.controller;

import com.toggery.POS.entity.Brand;
import com.toggery.POS.service.BrandService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by LJH on 2018/3/18
 */
@RestController
//默认返回json格式
@RequestMapping("/brand")
@Api(tags = "品牌数据")
public class BrandController {
    @Autowired
    BrandService service;


    @GetMapping("/list")
    public Map findAllBrand(){
        Map map = new HashMap() ;
        map.put("data",service.findAll());
        return map;
    }

    @GetMapping("/findbyname/{name}")
    public Map findByName(@PathVariable String name){
        Map map = new HashMap() ;
        map.put("allBrand",service.findByName(name));
        return map;
    }

    @GetMapping("/findlikename/{name}")
    public Map findLikeName(@PathVariable String name){
        Map map = new HashMap() ;
        map.put("allBrand",service.findLikeName(name));
        return map;
    }

    @PostMapping("/delete/{id}")
    public Map deleteById(@PathVariable int id){
        Map map = new HashMap();
        if(service.deleteByid(id)){
            map.put("result",true);
            map.put("msg","删除成功");
        }else {
            map.put("result",false);
            map.put("msg","删除失败");
        }
        return map;
    }

    @PostMapping("/update")
    @ResponseBody
    public Map updateBrand(Model model, @RequestBody(required=false) Map map1){
        Brand brand;
        brand = service.findById(Integer.parseInt(map1.get("id").toString()));
        brand.setBrandname(map1.get("brandname").toString());
        HashMap map = new HashMap();
        if(service.updateBrand(brand)){
            map.put("result",true);
            map.put("msg","更新成功");
        }else {
            map.put("result",false);
            map.put("msg","更新失败");
        }
        return map;
    }

    @PostMapping("/save")
    public Map saveBrand(Model model, @RequestBody(required=false) Map map1){
        Brand brand = new Brand();
        brand.setBrandname(map1.get("brandname").toString());
        HashMap map = new HashMap();
        if(service.save(brand)){
            map.put("result",true);
            map.put("result","保存成功");
        }else {
            map.put("result",false);
            map.put("result","保存失败");
        }
        return map;
    }

}
